package com.leyou.auth.config;

import com.leyou.common.auth.utils.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Map;

/**
 * 处理请求和响应的方法
 *
 * @version V1.0
 * @package com.leyou.gateway.config
 * @description:
 * @author: pc
 * @date: 2019/7/10 21:45
 */



@Data
@Slf4j
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties implements InitializingBean {

    /**
     * 公钥地址
     */
    private String pubKeyPath;
    /**
     * 私钥地址
     */
    private String priKeyPath;
    /**
     * 公钥
     */
    private PublicKey publicKey;
    /**
     * 私钥
     */
    private PrivateKey privateKey;
    /**
     * 用户需要的token信息
     * */
    private UserProperties user = new UserProperties();

    @Data
    public class UserProperties {
        private Integer expire;
        private String cookieName;
        private String cookieDomain;
        private Long minRemainTime;
        private Long refreshInterval;
    }

    private AppTokenProperties app = new AppTokenProperties();

    @Data
    public class AppTokenProperties {
        private int expire;
        private Long id;
        private String secret;
        private String headerName;

    }


    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            // 获取公钥和私钥
            this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
            this.privateKey = RsaUtils.getPrivateKey(priKeyPath);
        } catch (Exception e) {
            log.error("初始化公钥和私钥失败！", e);
            throw new RuntimeException(e);
        }
    }
}
